/*
 * @作者: 沙昊
 * @邮箱: shahao@zju.edu.cn
 * @功能: 
 * @包含算法: 
 * Copyright (c) 2023 by 沙昊, All Rights Reserved. 
 */
#include <vector>

int count = 0;

int direction[4][2] = {-1,0,0,1,1,0,0,-1};  
void dfs(vector<vector<int>> grid,int* visited,int cur_x, int cur_y){
    int m=grid.size();
    int n=grid[0].size();
    visited[cur_x][cur_y] = 1;
    for(int i=0; i<4; i++){
        int next_x = cur_x + direction[i][0];
        int next_y = cur_y + direction[i][1];
        if(next_x<0 || next_x>=m || next_y<0 || next_y>=n)continue;
        if(grid[next_x][next_y]==1 && visited[next_x][next_y]!=1){
            dfs(grid, visited,next_x,next_y );
        }
    }
    return ;
}

void main(vector<vector<int>> grid){
    int m = grid.size();
    int n = grid[0].size();
    int visited[m][n] = {0};
    for(int i=0; i<m;i++){
        for(int j=0; j<n; j++){
            if(grid[i][j]==1 && visited[i][j]!=1){
                dfs(grid, visited, i,j);
                count++;
            }
        }
    }

    return count;
}

